<?php

require_once(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR ."core".DIRECTORY_SEPARATOR."functions.php");
require_once("Activity.php");
require_once("Database.php");


class Event extends dbConn
{
	private $eventId;
	private $startTime,$endTime,$startDate,$endDate,$carpoolTime,$carpoolLocation;
	private $locationDetails,$meetingLocation,$meetingTime;
	private $eventName,$eventDescription;
	private $m1,$m2,$m3,$m4,$m5;
	private $dateCreated;

	private $activities = array();

	public function __construct($eventId)
	{
		if(is_int($eventId))
		{
			$this->eventId = $eventId;
			$this->fetchData();
			//$this->fetchActivities();
		}
		else
		{
			die("WRONG TYPE SUPPLIED FOR eventId");
		}
	}

	private function fetchData()
	{
		$query = "SELECT * FROM event WHERE eventId=$this->eventId LIMIT 1";

		$res = $this->query($query);
		if($this->num_rows($res) == 1)
		{
			$data = $this->fetch_array($res);
			$this->startTime = $data[E_ST];
			$this->endTime = $data[E_ET];
			$this->startDate = $data[E_SD];
			$this->endDate = $data[E_ED];
			$this->carpoolTime = $data[E_CT];
			$this->locationDetails = $data[E_LOC];
			$this->meetingLocation = $data[E_MLOC];
			$this->eventName = $data[E_NAME];
			$this->eventDescription = $data[E_D];
			$this->dateCreated = $data[E_DC];
		}
		else
		{
			die("FATAL ERROR : NO event with Id of $eventId exists");
		}
	}

	public static function exists($eventId)
	{
		$dbConn = new dbConn();
		if($dbConn->num_rows($dbConn->query("SELECT eventId FROM event WHERE eventId = $eventId")) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function getActivities()
	{
		$res = $this->query("SELECT activityId,title from activity WHERE event=$this->eventId");
		if($this->num_rows($res) > 0)
		{
			$array = array();
			while($data = $this->fetch_array($res))
			{
				$array[] = $data;
			}
			return $array;
		}else{
			return null;
		}
	}

	private function deleteActivities()
	{
		
	}

	private function update($fieldName,$value,$isInt = false)
	{
		if($isInt)
		{
			$this->query("UPDATE event SET $fieldName=$value WHERE eventId=$this->eventId");
		}
		else
		{
			$this->query("UPDATE event SET $fieldName='$value' WHERE eventId=$this->eventId");
		}
		$this->$fieldName = $value;
	}

	public static function getEventByDate($startDate)
	{
		$conn = new dbConn();
		$cnd = "startDate>='$startDate'";
		$query = "SELECT eventId,name FROM event WHERE ".$cnd." ORDER BY startDate";
		$res = $conn->query($query);
		if($conn->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $conn->fetch_array($res))
			{
				$array[] = $row;
			}
			return ($array);
		}
		else
		{
			return null;
		}
	}

	public static function getEventbyId($eventId)
	{
		$conn = new dbConn();
		$cnd = "eventId=$eventId LIMIT 1";
		$query = "SELECT * FROM event WHERE ".$cnd;
		return $conn->fetch_assoc($conn->query($query));
	}

	public static function getAllEventsOn($startDate)
	{
		$conn = new dbConn();
		$cnd = "startDate='$startDate'";
		$query = "SELECT eventId,name FROM event WHERE ".$cnd;
		$res = $conn->query($query);
		if($conn->num_rows($res) >= 1)
		{
			$array = array();
			while($row = $conn->fetch_array($res))
			{
				$array[] = $row;
			}
			return ($array);
		}
		else
		{
			return null;
		}
	}

	public static function checkForEvents($date)
	{
		$conn = new dbConn();
		$qry = $conn->query("SELECT eventId from event WHERE startDate='$date'");
		if($conn->num_rows($qry) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	//GETTERS AND SETTERS
	public function __get($var)
	{
		if(isset($this->$var))
			return $this->$var;
		return "NULL";
	}

	public function setEventName($eventName)
	{
		$this->update(E_NAME,$eventName);
	}

	public function setEventDescription($eventDescription)
	{
		$this->update(E_D,$eventDescription);
	}

	public function setEventStartTime($startTime)
	{
		$this->update(E_ST,$startTime);
	}
	
	public function setEventEndTime($endTime)
	{
		$this->update(E_ET,$endTime);
	}

	public function setEventStartDate($startDate)
	{
		$this->update(E_ST,$startDate);
	}

	public function setEventEndDate($endDate)
	{
		$this->update(E_ED,$endDate);
	}

	public function setCarpoolTime($carpoolTime)
	{
		$this->update(E_CT,$carpoolTime);
	}

	public function setLocationDetails($locationDetails)
	{
		$this->update(E_LOC,$locationDetails);
	}

	public function setMeetingLocation($meetingLocation)
	{
		$this->update(E_MLOC,$meetingLocation);
	}

	public function setEventManager($eventManager)
	{
		$this->update(E_M,$eventManager);
	}

	//static functions
	public static function getEvents($date)
	{
		$data = $this->fetch_array();
	}

	public static function deleteEvent($id)
	{
		$dbConn =  new dbConn();

		$cnd = "WHERE event=$id";
		if($dbConn->num_rows($dbConn->query("SELECT * FROM event_feedback ".$cnd)) > 0)
		{
			$dbConn->delete("DELETE FROM event_feedback ".$cnd);	
		}
			
		if($dbConn->num_rows($dbConn->query("SELECT * FROM equipment ".$cnd)) > 0)
		{
			$dbConn->delete("DELETE FROM equipment WHERE event=$id");
		}

		if($dbConn->num_rows($dbConn->query("SELECT * FROM equipment ".$cnd)) > 0)
		{
			$dbConn->delete("DELETE FROM todo WHERE event=$id");
		}
		
		Activity::deleteActivity($id,false);
		$dbConn->delete("DELETE FROM event WHERE eventId=$id");
	}

	public static function updateEvent($eventId,$eventName,$description,$startTime,$endTime,$startDate,$endDate,$meetingLocation,$meetingTime,$locationDetails,$carpoolTime,$carpoolLocation,$m1,$m2,$m3,$m4,$m5)
	{
		$m1 = ($m1=="null" ? null : $m1);
		$m2 = ($m2=="null" ? null : $m2);
		$m3 = ($m3=="null" ? null : $m3);
		$m4 = ($m4=="null" ? null : $m4);
		$m5 = ($m5=="null" ? null : $m5);
		$dbConn = new dbConn();
		$query = $dbConn->query("UPDATE event SET name='$eventName',description='$description',startTime='$startTime',endTime='$endTime',startDate='$startDate',endDate='$endDate',locationDetails='$locationDetails',carpoolLocation='$carpoolLocation',carpoolTime='$carpoolTime',meetingLocation='$meetingLocation',meetingTime='$meetingTime' WHERE eventId=$eventId");
        $dbConn->query("DELETE FROM manages WHERE event=$eventId");
		if(!is_null($m1))
		{
            $dbConn->insert("INSERT INTO manages (person,event) VALUES ($m1,$eventId)");
		}
		if(!is_null($m2))
		{
            $dbConn->insert("INSERT INTO manages (person,event) VALUES ($m2,$eventId)");
		}
		if(!is_null($m3))
		{
            $dbConn->insert("INSERT INTO manages (person,event) VALUES ($m3,$eventId)");
		}
		if(!is_null($m4))
		{
            $dbConn->insert("INSERT INTO manages (person,event) VALUES ($m4,$eventId)");
		}
		if(!is_null($m5))
		{
            $dbConn->insert("INSERT INTO manages (person,event) VALUES ($m5,$eventId)");
		}
	}
}


?>